/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.npmpackage.core.webservice;

import com.inspur.edp.web.common.utility.CommonUtility;
import com.inspur.edp.web.npmpackage.api.entity.NpmInstallParameter;
import com.inspur.edp.web.npmpackage.api.entity.NpmPackageResponse;
import com.inspur.edp.web.npmpackage.api.entity.NpmPackageResponseLevel;
import com.inspur.edp.web.npmpackage.api.entity.settings.NpmSettings;
import com.inspur.edp.web.npmpackage.api.webservice.NpmPackageInstallWebService;
import com.inspur.edp.web.npmpackage.core.npmsetting.NpmSettingConvertor;
import com.inspur.edp.web.npmpackage.core.npmsetting.NpmSettingEncryptService;
import com.inspur.edp.web.npmpackage.core.npmsetting.NpmSettingManager;
import com.inspur.edp.web.npmpackage.core.service.NpmPackageInstallService;

/**
 * npm 包安装webservice
 *
 * @author guozhiqi
 */
public class NpmPackageInstallWebServiceImpl implements NpmPackageInstallWebService {
    @Override
    public NpmPackageResponse install(NpmSettings npmSettings) {
        if (npmSettings == null) {
            throw new RuntimeException("npm 安装参数不能为空");
        }
        npmSettings = NpmSettingEncryptService.getInstance().deEncrypt(npmSettings);

        // 执行参数转换
        try {
            NpmInstallParameter npmInstallParameter = NpmSettingConvertor.convertFromNpmSetting(npmSettings);

            // 执行npm 安装
            NpmPackageResponse npmPackageResponse = NpmPackageInstallService.install(npmInstallParameter);

            try {
                // 保存最后的更新时间
                npmSettings.setLastUpdated(CommonUtility.getCurrentDateString());
                NpmSettingManager.saveNpmSetting(npmSettings);
            } catch (Exception ignored) {

            }

            return npmPackageResponse;
        } catch (Exception e) {
            NpmPackageResponse npmPackageResponse = new NpmPackageResponse();
            npmPackageResponse.setResponseLevel(NpmPackageResponseLevel.Error);
            npmPackageResponse.setSuccess(false);
            npmPackageResponse.setErrorMessage("npm install failed, 请检查配置或联系管理员!");
            return npmPackageResponse;
        }
    }

    /**
     * npm install命令执行 使用默认的npm配置信息
     *
     * @return 安装结果反馈
     */
    @Override
    public NpmPackageResponse install() {
        // 获取默认的npm 仓库配置
        NpmSettings npmSettings = NpmSettingManager.getNpmSetting(false);
        // 使用默认配置进行在线install
        return install(npmSettings);
    }

    /**
     * npm install命令执行
     *
     * @param installParameter install参数
     * @return 安装结果反馈
     */
    @Override
    public NpmPackageResponse installCheck(NpmSettings installParameter) {
        installParameter=NpmSettingEncryptService.getInstance().deEncrypt(installParameter);

        // 获取默认的npm 仓库配置
        NpmSettings npmSettings = NpmSettingManager.getNpmSetting(false);
        NpmInstallParameter npmInstallParameter = NpmSettingConvertor.convertFromNpmSetting(npmSettings);
        // 使用默认配置进行在线install
        return NpmPackageInstallService.installCheck(npmInstallParameter);
    }
}
